/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 14, 2013  1:08:07 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.nstda.aop
 * File Name : LoggingAspect.java
 * TODO
 */
package com.tis.nstda.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 14, 2013 : 1:08:07 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
//@Aspect
public class LoggingAspect {

	/**
	 * 4. AspectJ @Before In below example, the logBefore() method will be executed before the execution of customerBo interface, addCustomer() method.
	 * Date =Oct 25, 2013 : 4:38:31 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param joinPoint
	 */
	//@Before("execution(* com.tis.*.*)")
	public void logBefore(JoinPoint joinPoint) {
 
		System.out.println("logBefore() is running!");
		System.out.println("hijacked : " + joinPoint.getSignature().getName());
		System.out.println("******");
	}
	
	/**
	 * 5. AspectJ @After In below example, the logAfter() method will be executed after the execution of 
	 * 	  customerBo interface, addCustomer() method.
	 * Date =Oct 25, 2013 : 4:38:03 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param joinPoint
	 */
	//@After("execution(* com.tis.*.*)")
	public void logAfter(JoinPoint joinPoint) {
		 
		System.out.println("logAfter() is running!");
		System.out.println("hijacked : " + joinPoint.getSignature().getName());
		System.out.println("******"); 
	}
	
	/**
	 * 6. 	AspectJ @AfterReturning 
	 * 		In below example, the logAfterReturning() method will be executed after the execution of 
	 * 		customerBo interface, addCustomerReturnValue() method. 
	 * 		In addition, you can intercept the returned value with the “returning” attribute. 
	 * 		To intercept returned value, the value of the “returning” attribute (result) need to be same with the method parameter (result).
	 * Date =Oct 25, 2013 : 4:35:30 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param joinPoint
	 * @param result
	 */
	//@AfterReturning(pointcut = "execution(* com.tis.*.*)", returning= "result")
	public void logAfterReturning(JoinPoint joinPoint, Object result) {
		System.out.println("logAfterReturning() is running!");
		System.out.println("hijacked : " + joinPoint.getSignature().getName());
		System.out.println("Method returned value is : " + result);
		System.out.println("******");
 
	}
	
	/**
	 * 7. 	AspectJ @AfterReturning
	 * 		In below example, the logAfterThrowing() method will be executed if the customerBo interface, 
	 * 		addCustomerThrowException() method is throwing an exception.
	 * Date =Oct 25, 2013 : 4:34:08 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param joinPoint
	 * @param error
	 */
	//@AfterThrowing(pointcut = "execution(* com.tis.*.*)", throwing= "error")
	public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
		System.out.println("logAfterThrowing() is running!");
		System.out.println("hijacked : " + joinPoint.getSignature().getName());
		System.out.println("Exception : " + error);
		System.out.println("******");
	}
	
	/***
	 * 8. AspectJ @Around
	 * 	  In below example, the logAround() method will be executed before the customerBo interface, addCustomerAround() method, 
	 * 	  and you have to define the “joinPoint.proceed();” to control when should the interceptor return the control to 
	 * 	  the original addCustomerAround() method.
	 * 	  Date =Oct 25, 2013 : 4:33:00 PM 
	 * 	  CreateBy=Chatree Siangklom
	 * TODO
	 * @param joinPoint
	 * @throws Throwable
	 */
	//@Around("execution(* com.tis.*.*)")
	public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("logAround() is running!");
		System.out.println("hijacked method : " + joinPoint.getSignature().getName());
		System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));
	 
		System.out.println("Around before is running!");
		joinPoint.proceed(); //continue on the intercepted method
		System.out.println("Around after is running!");
	 
		System.out.println("******");
	 }
}
